//
//  ZWWebViewBaseViewController.m
//  WebviewProgress
//
//  Created by apple on 14-9-1.
//  Copyright (c) 2014年 thomas. All rights reserved.
//

#import "ZWWebViewBaseViewController.h"
#import "UINavigationController+SGProgress.h"

@interface ZWWebViewBaseViewController ()

@end

@implementation ZWWebViewBaseViewController
@synthesize bookWebView = _bookWebView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    _bookWebView = [[ZWWebView alloc] initWithFrame:self.view.bounds];
    _bookWebView.delegate = self;
    _bookWebView.bookDelegate = self;
    [self.view addSubview:_bookWebView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(progressEstimateChanged:) name:@"WebProgressEstimateChangedNotification" object:self.bookWebView.bookWebView];
     [self.bookWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.m_url]]];
    
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"WebProgressEstimateChangedNotification" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver: self name: @"offLineDownLoadSuccessWhenPreReaderNotification" object: nil];
}

#pragma mark progressEstimateNotification -
- (void)progressEstimateChanged:(NSNotification*)theNotification
{
    CGFloat progress = 0;
    if ([[theNotification userInfo] objectForKey:@"WebProgressEstimatedProgressKey"]) {
        progress = [[[theNotification userInfo] objectForKey:@"WebProgressEstimatedProgressKey"] floatValue];
    }else{
        progress = [self.bookWebView estimatedProgress];
    }
    if (progress>0) {
        [self.navigationController setSGProgressPercentage:progress*100];
    }
    if (progress>=1.0) {
        [self.navigationController finishSGProgress];
    }
}

#pragma mark -
#pragma mark UIWebViewDelegate -
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        //        [self.history addURL:request.URL.absoluteString withTitle:nil];
    }
    if ([request.URL.absoluteString isEqualToString:@"http://back/"]) {
        if ([webView canGoBack]) {
            [webView goBack];
        }
        return NO;
    }else if ([request.URL.absoluteString isEqualToString:@"http://reload/"]) {
        [webView reload];
        return NO;
    }
    
    return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
    
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    switch ([error code]) {
    }
}


@end
